package cn.edu.hatu.bookshop.controller;

import cn.edu.hatu.bookshop.entity.Book;
import cn.edu.hatu.bookshop.entity.Customer;
import cn.edu.hatu.bookshop.service.BookService;
import cn.edu.hatu.bookshop.service.ClassifyService;
import cn.edu.hatu.bookshop.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Locale;

@Controller
public class AdminController {
    private BookService bookService;
    private MessageSource messageSource;
    private ClassifyService classifyService;
    private CustomerService customerService;

    @Autowired
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    @Autowired
    public void setClassifyService(ClassifyService classifyService) {
        this.classifyService = classifyService;
    }

    @Autowired
    public void setMessageSource(MessageSource messageSource) {
        this.messageSource = messageSource;
    }

    @Autowired
    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }

    @RequestMapping("/uploadBookInput")
    public String uploadBookInput() throws IOException {
        File directory = new File("src/main/resources");
        String reportPath = directory.getCanonicalPath();
        System.out.println("path" + reportPath);
        return "Admin/UploadBook";
    }

    @RequestMapping("/uploadBook")
    @Transactional
    public String one(MultipartFile file, RedirectAttributes redirectAttributes, Locale locale, HttpServletRequest request) throws IOException {
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        String pubs = request.getParameter("pubs");
        Double price = Double.parseDouble(request.getParameter("price"));
        int count = Integer.parseInt(request.getParameter("count"));
        String classifyName = request.getParameter("classifyName");
        int classifyId = classifyService.getClassifyId(classifyName);
        String bookShelf = request.getParameter("bookShelf");
        Book book = new Book(pubs, bookName, author, count, classifyId, price, bookShelf);
        bookService.addBook(book);
        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("msg", messageSource.getMessage("file.empty", null, locale));
            return "redirect:uploadBookInput";
        }
        redirectAttributes.addFlashAttribute("name", file.getOriginalFilename());
        File directory = new File("src/main/resources");
        String reportPath = directory.getCanonicalPath();
        String realPath = reportPath + "/static/images/" + classifyName;
        File destFile = new File(realPath, file.getOriginalFilename());
        if (!destFile.exists()) {
            file.transferTo(destFile);
        }
        System.out.println("classifyName:" + classifyName);
        return "redirect:browseBooks";
    }

    @RequestMapping("/showBook")
    public String showBook(Model model, HttpServletRequest request) {
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        Book book = bookService.findById(bookId);
        model.addAttribute("book", book);
        String classifyName = classifyService.getClassifyName(book.getClassifyId());
        model.addAttribute("classifyName", classifyName);
        return "Admin/ShowBook";
    }

    @RequestMapping("/browseBooks")
    public String showBooks(Model model, HttpServletRequest request) {
        int classifyId = 1;
        if (request.getParameter("classifyId") != null) {
            classifyId = Integer.parseInt(request.getParameter("classifyId"));
        }
        List<Book> books = bookService.getBooksByClassifyId(classifyId);
        model.addAttribute("books", books);
        String classifyName = classifyService.getClassifyName(classifyId);
        model.addAttribute("classifyName", classifyName);
        return "Admin/BrowseBooks";
    }

    @RequestMapping("/modifyBookInput")
    public String modifyBookInput(Model model, HttpServletRequest request) {
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        Book book = bookService.findById(bookId);
        model.addAttribute("book", book);
        return "Admin/ModifyBook";
    }

    @RequestMapping("/modifyBook")
    @Transactional
    public String modifyBook(HttpServletRequest request) {
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        String bookName = request.getParameter("bookName");
        String author = request.getParameter("author");
        String pubs = request.getParameter("pubs");
        int count = Integer.parseInt(request.getParameter("count"));
        Double price = Double.parseDouble(request.getParameter("price"));
        String classifyName = request.getParameter("classifyName");
        int classifyId = classifyService.getClassifyId(classifyName);
        String bookShelf = request.getParameter("bookShelf");
        Book book = new Book(pubs, bookName, author, count, classifyId, price, bookShelf);
        bookService.modifyBook(book);
        return "redirect:browseBooks";
    }

    @RequestMapping("/showCustomers")
    public String showCustomers(Model model) {
        List<Customer> customers = customerService.getAll();
        model.addAttribute("customers", customers);
        return "Admin/ShowCustomers";
    }

    @RequestMapping("/delCustomer")
    public String delCustomer(HttpServletRequest request) {
        int customerId = Integer.parseInt(request.getParameter("customerId"));
        customerService.delById(customerId);
        return "redirect:showCustomers";
    }
    @RequestMapping("/delBook")
    @Transactional
    public String delBook(HttpServletRequest request){
        int bookId = Integer.parseInt(request.getParameter("bookId"));
        bookService.delBookById(bookId);
        return "redirect:browseBooks";
    }


}
